﻿using Bardent.Weapons.Components.ComponentData;
using Bardent.Weapons.Components.ComponentData.AttackData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Bardent.Weapons.Components
{

    

    public class ActionHitBox:WeaponComponent<ActionHitBoxData,AttackActionHitBox>
    {

        /// <summary>
        /// 核心组件
        /// </summary>
        private CoreComp<Movement> movement;

        //偏移量
        private Vector2 offset;

        /// <summary>
        /// 检测到的物体
        /// </summary>
        private Collider2D[] detected;

        /// <summary>
        /// 
        /// </summary>
        public event Action<Collider2D[]> OnDetectedCollider2D;

        private void HandleAttackAciton()
        {
            //获取偏移量
            offset.Set(
                transform.position.x + (currentAttackData.HitBox.center.x * movement.Comp.FacingDirection),
                transform.position.y+currentAttackData.HitBox.center.y
                );

            Debug.Log(currentAttackData.HitBox.size);
            detected = Physics2D.OverlapBoxAll(offset,currentAttackData.HitBox.size,0f,data.DetectableLayers);

            if (detected.Length==0)
            {
                return;
            }

            OnDetectedCollider2D?.Invoke(detected);

            foreach (var item in detected)
            {
                Debug.Log(item.name);
            }

        }

        protected override void Start()
        {
            base.Start();
            movement = new CoreComp<Movement>(Core);
            eventHandler.OnAttackAction += HandleAttackAciton;
        }



        protected override void OnDestroy()
        {
            base.OnDestroy();
            eventHandler.OnAttackAction -= HandleAttackAciton;
        }


        private void OnDrawGizmos()
        {
            if (data == null)
            {
                return;
            }

            foreach (var item in data.AttackData)
            {
                if (!item.Debug)
                {
                    continue;
                }
                Gizmos.DrawWireCube(transform.position + (Vector3)item.HitBox.center, item.HitBox.size);
            }
        }

        private void OnDrawGizmosSelected()
        {
            Debug.Log("绘制准备");
            if (data==null)
            {
                return;
            }

            foreach (var item in data.AttackData)
            {
                if (!item.Debug)
                {
                    continue;
                }
                Debug.Log("绘制");
                Gizmos.DrawWireCube(transform.position + (Vector3)item.HitBox.center,item.HitBox.size);
            }
        }

    }
}
